package Core;
/*
 *  J2Egn version 2.6.28
 *  Author: Van Tan Tai 2010-2011
 *  Core center event
 */
import javax.microedition.lcdui.Graphics;

import Const.GConfig;
import Pages.State;
import Pages.Static;

public class GLives {

	public interface VENDOR {
		int UNKNOWN = 0;
		int NOKIA = 1;
		int SONY = 2;
		int BLACKBERRY = 3;
		int FPT = 4;
		int MOTOROLA = 5;
		int BENQ = 6;
		int SAMSUNG = 7;
		//--------------------
		int NOKIA_E63 = 21;
		int NOKIA_E61 = 22;
		int F99 = 41;
	}
	
	/* core variable */
	public static int vendorDevice = VENDOR.UNKNOWN;
	public static int vendorSeries = VENDOR.UNKNOWN;
	public static int ScreenWidth, ScreenHeight;
	public static boolean isTouchable = false;
	public static boolean isQwerty = false;
	public static GLives ins = null;
	
	/* static variable */
	public boolean isGameKey = GConfig.isModeGame;
	public String phoneName = "taVan";
	public String version = "1.0";
	public String Partner = "Jamo";
//	public int iframe = 0;
	
	/* engine element */
	public Slide currSlide = null, prevSlide = null;
	public MRecord record = new MRecord(GConfig.recordStoreName);
	public RGBFont bmf_normal = new RGBFont("/font/medium.pak");
	public RGBFont bmf_bold = new RGBFont("/font/big.pak");
	
	/* local variable */
	private short IDlinkSlide = -3;
	private boolean isFrozen = false; // loading, exit, execute, v.v...
	private long ltimeStart = 0;
	
	public GLives(){
		ins = this;
		Sound.ins = new Sound();
		Sound.ins.setEnable(false);
		bmf_bold.m_hasBorder = true;
	}
	protected void destroy(){
		isFrozen = true;
		Static.destroy();
		Touch.destroy();
		freeMem();
		Sound.ins.destroy();
		Sound.ins = null;
		record.destroy();
		record = null;
//		bmf_normal.destroy();
//		bmf_normal = null;
//		bmf_bold.destroy();
//		bmf_bold = null;
	}
	private void freeMem() {
		if(GLives.ins!= null && GLives.ins.currSlide!=null){
			if(GLives.ins.currSlide.subSl != null)
				GLives.ins.currSlide.subSl.destroy();
			GLives.ins.currSlide.destroy();
			System.gc();
		}
	}
	public void setState(short newState, boolean isSubState){
		isFrozen = true;
		prevSlide = currSlide;
		if(!isSubState)	freeMem();
		currSlide = State.Loading(newState);
		if(currSlide == null) { System.out.println("INVALID STATE!"); return; }
		if(newState == State.BODY_CONTINUE || newState == State.BODY_NEW) newState = State.BODY;
		currSlide.id = newState;
		if(isSubState){
			prevSlide.subSl = currSlide;
			currSlide.owned = prevSlide;
		}
		initState();
		currSlide.init();
		currSlide.registerTouch();
		isFrozen = false;
	}
	public void setState(Slide sl, boolean needInit){
		isFrozen = true;
		prevSlide = currSlide;
		currSlide = sl;
		initState();
		if(needInit) sl.init();
		currSlide.registerTouch();
		isFrozen = false;
	}
	private void initState(){
		KEY.resetKeys();
		Touch.releaseTouchArea();
		currSlide.executeInnerSize();
		isGameKey = GConfig.isModeGame;
	}
	public int getState(){
		return currSlide.id;
	}
	protected void init(boolean B, String S, int F, String V, boolean Q){
		// get phone information
		isTouchable = B;
		version = V;
		isQwerty = Q;
		// get vendor 
		String s = S;
		phoneName = s;
		int kind = F;
		if (kind == -20)
			GLives.vendorDevice = VENDOR.MOTOROLA;
		if (s == null || s.length() == 0)
			GLives.vendorDevice = VENDOR.UNKNOWN;
		else if (s.indexOf("Nokia") != -1)
			GLives.vendorDevice = VENDOR.NOKIA;
		else if (s.indexOf("Samsung") != -1)
			GLives.vendorDevice = VENDOR.SAMSUNG;
		else if (s.indexOf("Sony") != -1)
			GLives.vendorDevice = VENDOR.SONY;
		else if (s.indexOf("RIM") != -1)
			GLives.vendorDevice = VENDOR.BLACKBERRY;
		else if(s.indexOf("SIE-EL71") != -1)
			GLives.vendorDevice = VENDOR.BENQ;
		else if (s.indexOf("MAUI") != -1){
			GLives.vendorDevice = VENDOR.FPT;
			isQwerty = true;
		}
		// get device name
		if (s.indexOf("NokiaE63") != -1)
			GLives.vendorSeries = VENDOR.NOKIA_E63;
		else if (GLives.vendorDevice == VENDOR.FPT)
			GLives.vendorSeries = VENDOR.F99;		// get Application information
		else if (s.indexOf("NokiaE61") != -1){
			GLives.vendorSeries = VENDOR.NOKIA_E61;
			isQwerty = true;
		}
		// init Application
		Static.init();
	}
	
	protected void init(int W, int H){
		ScreenWidth = W;
		ScreenHeight = H;
		setState(State.THE_FISRT_SCREEN_PAGE_OF_APP, false);
	}
	
	public void startTextInput(){
		isGameKey = false;
	}
	protected void onkeyDown(int keyCode) {
		IDlinkSlide = currSlide.id;
		currSlide.onkeyDown(KEY.getKey(keyCode, KEY.EVENT_PRESS));
	}
//	protected void keyRepeated(int keyCode) {
//		onkeyDown(keyCode);
//	}
	protected void onkeyUp(int keyCode) {
		if(IDlinkSlide != currSlide.id){
			//System.out.println("HAS JUST CHANGE STATE!!!");
			return;
		}
		currSlide.onkeyUp(KEY.getKey(keyCode, KEY.EVENT_RELEASE));
	}
	protected void touchDown(int x, int y) {
		Touch.isPressed = true;
		IDlinkSlide = currSlide.id;
		ltimeStart = System.currentTimeMillis();
		Touch.x = Touch.xDown =  x;
		Touch.y = Touch.yDown =  y;
		Touch.IDdown = Touch.click(x, y);
		currSlide.touchDown(Touch.IDdown, x, y);
	}
	protected void touchDrag(int x, int y) {
		if(IDlinkSlide != currSlide.id){
			//System.out.println("HAS JUST CHANGE STATE!!!");
			return;
		}
		Touch.x = x;
		Touch.y = y;
		currSlide.touchDrag(x, y);
	}
	protected void touchUp(int x, int y) {
		Touch.isPressed = false;
		Touch.x = Touch.xUp =  x;
		Touch.y = Touch.yUp =  y;
		if(IDlinkSlide != currSlide.id){
			//System.out.println("HAS JUST CHANGE STATE!!!");
			return;
		}
		Touch.IDcurr = Touch.click(x, y);
		int dx = (x - Touch.xDown); if(dx < 0) dx = -dx; // <=> int m = dx >> 31; dx = (dx + m) ^ m;
		int dy = (y - Touch.yDown); if(dy < 0) dy = -dy;
		Touch.holdTime = (int)(System.currentTimeMillis() - ltimeStart);
		
		if(Touch.IDcurr == Touch.IDdown && (Touch.IDcurr != Touch.NOTHING || (Touch.IDcurr == Touch.NOTHING && dx <= 1 && dy <= 1))){
			currSlide.Click(Touch.IDcurr, x, y);
			Touch.IDprev = Touch.IDcurr;
		}else if(dx > 1 || dy > 1){
			currSlide.touchUp(Touch.IDcurr, x, y, Touch.holdTime, Touch.xDown, Touch.yDown);
		}
		
		currSlide.touchUp(Touch.IDcurr, x, y);
		
	}
	
	protected void pause(){
		if(Sound.ins != null) Sound.ins.stopAll();
		if(!isFrozen && (currSlide.id == State.BODY))
			currSlide.changeSubState(State.INBODY_MENU);
	}
	
	protected void resume(){
		if(currSlide.id != State.INBODY_MENU && currSlide.id != State.IGP && Sound.ins != null)
			Sound.ins.resume();
	}
	
	protected void onResize(int width, int height){
		System.out.println("---RESIZE");
		ScreenWidth = width;
		ScreenHeight = height;
		
		if(currSlide != null){
			if(currSlide.owned != null){
				currSlide.owned.executeInnerSize();
				currSlide.owned.onResize();
			}
			Touch.releaseTouchArea();
			currSlide.executeInnerSize();
			currSlide.onResize();
			Touch.releaseTouchArea();
			currSlide.registerTouch();
		}
	}
	
	protected void paint(Graphics g){
		if(isFrozen) return;
		if(currSlide.owned != null)
			currSlide.owned.paint(g);
		currSlide.paint(g);
		State.draw_DEBUG_Info(g);
	}

	protected void update(){
		if(isFrozen) return;
		KEY.update();
		currSlide.update();
	}
}
